<?php
namespace Home\Controller;
use Think\Controller;
class PicapiController extends Controller {
    public function index(){
		 $page_size = isset($_GET['page_size']) ? intval($_GET['page_size']) : '1';
                 $page = isset($_GET['page']);
                 $offset = $pagesize * ($page - 1); //计算记录偏移量
		 $data = M('photo')->limit($offset . ',' . $pagesize)->order('id desc')->select();
	     foreach($data as $key=>$val){
			
		   preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/',$val['content'],$match);
		  
                   foreach($match[1] as $k=>$v){
                      $match[1][$k] = 'https://phone.gyhxxs.com'.$v;
                   }
                    $data[$key]['imgs'] = $match[1];
                    if($data[$key]['imgs']){
                        $data[$key]['image'] = 'https://phone.gyhxxs.com'.$val['image'];
                    }
                 
                   if($val['imglist']){
                      $data[$key]['imgs'] = explode(',',substr($val['imglist'], 0,  strlen($val['imglist'])-1));
                    if($val['imglist']){
                      $imgList = explode(',',substr($val['imglist'], 0,  strlen($val['imglist'])-1));
                      foreach($imgList as $k=>$v){
                          $data[$key]['content']  = $data[$key]['content'] .' <img src=\"https://'.$v.'\" alt="" />';
                      }
                   }
                      $data[$key]['image'] = 'https://'.$val['image'];
                   }
                
		 }
				
				echo  json_encode($data);
	}
	
	public function view(){
		$id = I('get.id');
		$data = M('photo')->where(array('id'=>$id))->find();
	    
			
		preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/',$data['content'],$match);
		
		$data['imgs'] = $match[1];
		foreach($data['imgs'] as $key=>$val){
                 $data['content'] = str_replace($val, 'https://phone.gyhxxs.com'.$val, $data['content']);
			
		}
              
                 if($data['imglist']){
                      $imgList = explode(',',substr($data['imglist'], 0,  strlen($data['imglist'])-1));
                    
                      foreach($imgList as $k=>$v){
                         $v = str_replace(' ', '%20', $v);
                         $v = str_replace(',', '%2C', $v);
                          $data['content']  = $data['content'] .' <img src="https://'.$v.'" alt="" />';
                      }
                   }
                
		$data['gzlist'] = M('photo_guanzhu')->where(array('vid'=>$id))->select();   
		echo  json_encode($data);
	}
}